package com.yuandian.bpm.bi.modules.quartz.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportTaskInfo;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 自定义报表 工具类
 */
@Slf4j
public class CronUtil {

	public static String getCronExpression(ConfReportTaskInfo confReportTaskInfo) {
		String cronExpression = "";
		// 任务执行频率 0:执行一次 1：重复执行 2: 表达式设置
		Integer taskExecuteFrequency = confReportTaskInfo.getTaskExecuteFrequency();
		String taskCronExpressionStr = confReportTaskInfo.getTaskCronExpressionStr();
		if (StringUtils.isEmpty(taskCronExpressionStr)) { return null;}

		try {
			if (1 == taskExecuteFrequency) {
				// 重复执行 1_MINUTE, 1_HOUR, 1_DAY
				int value = Integer.parseInt(taskCronExpressionStr.split("_")[0]);
				String unit = taskCronExpressionStr.split("_")[1];
				switch (unit) {
					case "MIN":
						cronExpression = "0 */" + value + " * * * ?";
						break;
					case "HOUR":
						cronExpression = "0 0 */" + value + " * * ?";
						break;
					case "DAY":
						cronExpression = "0 0 0 */" + value + " * ?";
						break;
					case "WEEK":
						cronExpression = "0 0 0 */" + (value*7) + " * ?";
						break;
				}
			} else if (2 == taskExecuteFrequency) {
				return taskCronExpressionStr;
			}

		} catch (Exception e) {
			log.error("自定义报表-获取表达式报错");
			e.printStackTrace();
		}

		return cronExpression;
	}



	/**
	 * 基于 nowDateTime 往后延迟 minute 分钟
	 * @param nowDateTime 时间
	 * @param minute 延迟的分钟数
	 * @return 返回延迟后的时间 yyyy-MM-dd HH:mm:ss
	 */
	public static String getAfterMinuteStr(String nowDateTime, int minute){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(nowDateTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.MINUTE, minute);
		c.set(c.SECOND, 00);
		return sdf.format(c.getTime());
	}


	/**
	 * 往后延后多少天
	 * @param timeStr	时间参数
	 * @param date	要延后的天数
	 * @return 结果
	 */
	public static String getAfterDay(String timeStr, int date) {
		Date nowTime = stringFormatDate(timeStr);
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.DATE, +date);
		return yyyy_MM_dd_HH_mm_ss(c.getTime());
	}


	/**
	 * 把日期转换为（yyyy-MM-dd HH:mm:ss）格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String yyyy_MM_dd_HH_mm_ss(Date date) {
		if (date != null) {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf1.format(date);
		}
		return null;
	}


	/**
	 * 往后延后多少小时
	 * @param timeStr	时间参数
	 * @param hour	要延后的小时
	 * @return 结果
	 */
	public static String getAfterHour(String timeStr, int hour) {
		Date nowTime = stringFormatDate(timeStr);
		Calendar c = Calendar.getInstance();
		c.setTime(nowTime);
		c.add(c.HOUR, +hour);
		return yyyy_MM_dd_HH_mm_ss(c.getTime());
	}

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";


	/**
	 * 把字符串转换为日期（格式：yyyy-MM-dd HH:mm:ss）
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date stringFormatDate(String dateStr){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}



	/**
	 * 获取前几天的同一时间值
	 * @param time 时间
	 * @param days 天数
	 * @return
	 */
	public static String getBeforeDayTime(String time,Integer days) {
		boolean timeExits = ObjectUtil.isNotEmpty(time);
		Calendar calendar = Calendar.getInstance();
		if(timeExits){
			DateTime parse = DateUtil.parse(time);
			calendar.setTime(parse);
		}else {
			calendar.setTime(new Date());
			calendar.set(Calendar.HOUR_OF_DAY,0);
			calendar.set(Calendar.MINUTE,0);
			calendar.set(Calendar.SECOND,0);
		}
		calendar.add(Calendar.DATE, -days);
		Date beforeTime = calendar.getTime();
		return yyyy_MM_dd_HH_mm_ss(beforeTime);
	}



}
